package com.yzf.enterprise.framework.mybatisplus;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.extension.incrementer.H2KeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.google.common.collect.Lists;
import com.yzf.enterprise.framework.datasource.DynamicDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Configuration
@MapperScan("com.yzf.enterprise.project.mapper")
public class MybatisPlusConfig {

    private static final List<String> IGNORE_TENANT_TABLES = Lists.newArrayList();

//    /**
//     * mybatis-plus SQL执行效率插件
//     */
//    @Bean
//    public PerformanceInterceptor performanceInterceptor() {
//        return new PerformanceInterceptor();
//    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

//    @Bean
//    public IdGenerator getIdGenerator() {
//        return new CommonSelfIdGenerator();
//    }

    /**
     * 注入主键生成器
     */
    @Bean
    public IKeyGenerator keyGenerator(){
        return new H2KeyGenerator();
    }

//    @Bean(name = "master")
//    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
//    public AtomikosDataSourceBean masterDataSource() {
//        return new AtomikosDataSourceBean();
//    }
//
//    @Bean(name = "slave")
//    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.slave")
//    public AtomikosDataSourceBean slaveDataSource() {
//        return new AtomikosDataSourceBean();
//    }
//
//    /**
//     * 动态数据源配置
//     *
//     * @return
//     */
//    @Bean
//    @Primary
//    public DataSource multipleDataSource(@Qualifier("master") DataSource master,
//                                         @Qualifier("slave") DataSource slave) {
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put("master", master);
//        targetDataSources.put("slave", slave);
//        DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource();
//        dynamicDataSource.addDataSource("master",master);
//        dynamicDataSource.addDataSource("slave",slave);
//        return dynamicDataSource;
//    }
//
    @Autowired
    private MybatisPlusProperties properties;

    @Bean("sqlSessionFactory")
    @ConditionalOnBean(DynamicRoutingDataSource.class)
    @Primary
    public SqlSessionFactory sqlSessionFactory(DynamicRoutingDataSource dynamicRoutingDataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dynamicRoutingDataSource);

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setMapperLocations(this.properties.resolveMapperLocations());
        //PerformanceInterceptor(),OptimisticLockerInterceptor()
        //添加分页功能
        sqlSessionFactory.setPlugins(new Interceptor[]{
                paginationInterceptor()
        });
//        sqlSessionFactory.setGlobalConfig(globalConfiguration()); //注释掉全局配置，因为在xml中读取就是全局配置
        return sqlSessionFactory.getObject();

    }

    @Bean
    @Primary
    public SqlSessionTemplate SqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
